package com.resou.reader.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.resou.reader.R;

import org.jetbrains.annotations.NotNull;

import io.reactivex.annotations.Nullable;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:TODO 简易版 ，后续优化
 * CreateTime: 2019-03 -16
 **/
public class RowView extends FrameLayout {
    LinearLayout llRowView;
    ImageView title_Icon;
    ImageView title_Icon2;
    TextView title;
    int icon = 0;
    TextView title_content;
    String content = "";
    View mCircle;
    boolean isShowNew = false;

    public RowView(Context context) {
        super(context);
    }

    @SuppressLint("WrongViewCast")
    public RowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.rowview_item, this);
        llRowView = (LinearLayout) findViewById(R.id.llRowView);
        mCircle = (View) findViewById(R.id.mCircle);
        title_Icon = (ImageView) findViewById(R.id.ic_titleIcon);
        title_Icon2 = (ImageView) findViewById(R.id.ic_titleIcon2);
        title = (TextView) findViewById(R.id.tvTitles);
        title_content = (TextView) findViewById(R.id.tvContent);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RowView);
        initView(array);
    }

    public RowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


    }


    /**
     * 初始化布局视图
     *
     * @param array
     */
    private void initView(TypedArray array) {
        try {
            icon = array.getResourceId(R.styleable.RowView_rowViewTitleIcon, 0);
            if (icon != 0) {
                title_Icon.setVisibility(View.VISIBLE);
                title_Icon.setBackgroundResource(icon);
            } else {
                title_Icon.setVisibility(View.GONE);
            }
            content = array.getString(R.styleable.RowView_rowViewContent);
            if (!TextUtils.isEmpty(content)) {
                title_content.setVisibility(View.VISIBLE);
                title_content.setText(content);
            }
            isShowNew = array.getBoolean(R.styleable.RowView_rowViewisShowNews, false);
            if (isShowNew) {
                mCircle.setVisibility(View.VISIBLE);
            }
            title.setText(array.getString(R.styleable.RowView_rowViewTitle));
        } catch (Exception ex) {
            Log.e("Exception:", "RowView: " + ex.getMessage());
        }
    }

    /**
     * 设置主标题
     *
     * @param params
     */
    public RowView setTitle(String params) {
        title.setText(params);
        return this;
    }

    /**
     * 设置副标题
     *
     * @param params 内容
     * @return
     */
    public RowView setContent(String params) {
        if (title_content.getVisibility() == View.GONE) {
            title_content.setVisibility(View.VISIBLE);
        }
        title_content.setText(params);
        return this;
    }

    /**
     * 设置副标题颜色
     *
     * @param color
     * @return
     */
    public RowView setContentColor(int color) {
        title_content.setTextColor(getResources().getColor(color));
        return this;
    }


    /**
     * 设置图片图片地址
     *
     * @param url
     * @return
     */
    public RowView setUrlPic(String url) {
        title_Icon2.setVisibility(View.VISIBLE);
        Glide.with(this).load(url == null ? "" : url).into(title_Icon2);
        return this;
    }

    /**
     * 设置图片图片地址
     *
     * @param url
     * @return
     */
    public RowView setUrlPic(int url) {
        title_Icon2.setVisibility(View.VISIBLE);
        title_Icon2.setImageResource(url);
        return this;
    }

    /**
     * 是否显示消息红点
     *
     * @return true 显示  false  不显示
     */
    public RowView isShowNewPoints(boolean isShow) {
        mCircle.setVisibility(isShow ? View.VISIBLE : View.GONE);
        return this;
    }


    /**
     * sp
     */
    public int sp2x(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                sp, getResources().getDisplayMetrics());
    }
}
